﻿package view.components.portfolio 
{
	import br.com.stimuli.loading.BulkLoader;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.filters.ColorMatrixFilter;
	import org.casalib.display.CasaMovieClip;
	import org.casalib.display.CasaSprite;
	import view.components.utils.UIEvent;
	
	import gs.TweenLite;
	import gs.easing.Cubic;
	
	import view.components.utils.ColorMatrix;
	
	/**
	 * ...
	 * @author Lobanov
	 */
	public class ItemThumbnails extends CasaMovieClip
	{
		static public var OVER_DEPTH : String = "on over sending deptn of thumb";
		
		private var _imageContainer:CasaSprite;
		private var _maskImage:CasaSprite;
		private var _id:Number;
		private var _bm:Bitmap;
		private var _forMotionChange:Sprite;
		
		public function ItemThumbnails($linkImage:String, $id:String ) 
		{
			_id = $id;
			_forMotionChange = new Sprite();
			_imageContainer = new CasaSprite();
			
			_maskImage = new CasaSprite();
			_maskImage.graphics.beginFill(0xffffff, 1);
			_maskImage.graphics.drawRect(0, 0, 220, 160);
			_maskImage.graphics.endFill();	
			_maskImage.x = _maskImage.y = 15;
			_maskImage.width = 190;
			_maskImage.height = 130;
			
			addChild(_imageContainer);
			addChild(_maskImage);
			_imageContainer.mask = _maskImage;
			
			loadImage($linkImage);
		}
		
		private function loadImage($link:String):void 
		{
			var bLoader:BulkLoader = new BulkLoader("thumb loader" + _id);
			bLoader.add($link, { id:"image" } );
			bLoader.addEventListener(BulkLoader.COMPLETE, onCompleteHandler);
			bLoader.addEventListener(BulkLoader.PROGRESS, onProgressHandler);
			bLoader.start();
		}
		
		private function onProgressHandler(evt:ProgressEvent):void {
			//trace("Loaded" , evt.bytesLoaded," of ",  evt.bytesTotal);
		}
		
		private function onCompleteHandler(evt:ProgressEvent):void 
		{		
			var bd:Bitmap = (evt.target as BulkLoader).getBitmap("image", true);
			_imageContainer.addChild(bd);
			
			var cm:ColorMatrix = new ColorMatrix();
			cm.colorize(0xeeedc8, 1);
			_imageContainer.filters = [ new ColorMatrixFilter( cm.matrix ) ];
			
			(evt.target as BulkLoader).clear();
			(evt.target as BulkLoader).removeEventListener(BulkLoader.COMPLETE, onCompleteHandler);
			(evt.target as BulkLoader).removeEventListener(BulkLoader.PROGRESS, onProgressHandler);
			
			_maskImage.buttonMode = true;
			_maskImage.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
			_maskImage.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
			_maskImage.addEventListener(MouseEvent.MOUSE_OUT, clickHandler);
		}
		
		/**
		 * Events Thumbnails
		 * @param	e
		 */
		private function overHandler(e:MouseEvent):void 
		{
			TweenLite.to(_maskImage, .5, { x:0, y:0, width:220, height:160, ease:Cubic, onUpdate:updateColorize } );
			TweenLite.to(_forMotionChange, .5, { alpha:0, onUpdate:updateColorize } );
			dispatchEvent( new UIEvent(OVER_DEPTH, this) );
		}
		
		private function outHandler(e:MouseEvent):void 
		{
			TweenLite.to(_maskImage, .5, { x:15, y:15, width:190, height:130, ease:Cubic, onUpdate:updateColorize } );
			TweenLite.to(_forMotionChange, .5, { alpha:1, onUpdate:updateColorize } );
		}
		
		private function clickHandler(e:MouseEvent):void 
		{
			
		}
		
		private function updateColorize($amount:Number = 0):void 
		{			
			var cm:ColorMatrix = new ColorMatrix();
			cm.colorize(0xeeedc8, _forMotionChange.alpha);
			_imageContainer.filters = [ new ColorMatrixFilter( cm.matrix ) ];
		}
		
		public function destroyThumb():void 
		{
			_forMotionChange = null;
			_maskImage.removeEventListeners();
			_maskImage.removeChildren(true, true);
			_imageContainer.removeChildren(true, true);
		}
	}
}